package com.yx.edu.resolver;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * @description:
 * @author: yx
 * @date: 2021/8/10 11:15
 */
@ControllerAdvice
public class GlobalExceptionResolver {

    //指定捕获异常的类型
    @ExceptionHandler(Exception.class)
    //可以将捕获的异常类型或父类当成方法形参传入
    public ModelAndView handlerException(Exception e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        modelAndView.addObject("msg", e.getMessage());
        return modelAndView;
    }
}
